package com.ruoyi.web.controller.Ca;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.Ca;
import com.ruoyi.web.domain.Vo.CaVo;
import com.ruoyi.web.service.CaLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

@Api(value = "注册登录功能")
@RequestMapping("/ca")
@RestController
public class PatientLoginController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private CaLoginService caLoginService;

    @ApiOperation(value = "测试连接")
    @GetMapping("findtest")
    public AjaxResult findTest(){
        return AjaxResult.success("测试成功");
    }

    @ApiOperation(value = "根据手机号查询用户是否存在")
    @GetMapping("/number/{number}")
    public AjaxResult findByNumber(@PathVariable String number){
        caLoginService.findBy(number);
        return AjaxResult.success("用户已注册");
    }

    /*
     * 注册方法,账号注册
     * */
    @ApiOperation(value = "注册功能")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Ca ca){
        //判断姓名是否为空
        if (!StringUtils.isEmpty(ca.getName())){
            String name = ca.getName();
        } else {
            return AjaxResult.error("用户名不能为空");
        }
        //判断手机号
        if (!StringUtils.isEmpty(ca.getPhonenumber())){
            String phoneNumber = ca.getPhonenumber();
            //检测用户是否注册过
            boolean b = caLoginService.findBy(phoneNumber);
            if (b){
                return AjaxResult.error("用户已存在");
            }
        } else {
            return AjaxResult.error("手机号不能为空");
        }
        // 判断密码
        if (!StringUtils.isEmpty(ca.getPassword())){
            String password = ca.getPassword();
        } else {
            return AjaxResult.error("密码不能为空");
        }
        //判断性别
        if (!StringUtils.isEmpty(ca.getPassword())){
            String sex = ca.getSex();
        } else {
            return AjaxResult.error("性别不能为空");
        }
        caLoginService.insertCa(ca);
        return AjaxResult.success("注册成功");
    }

    /*
    * 发送验证码方法
    * */
    @ApiOperation(value = "发送验证码")
    @GetMapping("/captcha/{phoneNumber}")
    public AjaxResult captcha(@PathVariable String phoneNumber){
        //生成四位数验证码
        double d = (Math.random() + 1) * 1000;
        String s = d + "";
        String captcha = s.substring(0,4);
        //将验证码放入缓存
        redisTemplate.opsForValue().set(phoneNumber,captcha,300, TimeUnit.SECONDS);
        System.out.println(captcha);

        //TODO 发送验证码到手机


        return AjaxResult.success(captcha);
    }

    /**
     * 登录方法,手机号登录,验证码
     *
     * @param caVo 登录对象
     * @return 结果
     */
    @ApiOperation(value = "手机号登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody CaVo caVo)
    {
        String phoneNumber = caVo.getPhonenumber();
        String captcha = caVo.getCode();

        //验证验证码是否正确
        boolean b = caLoginService.verification(phoneNumber, redisTemplate, captcha);

        if (!b){
            return AjaxResult.error("data","验证码不正确");
        }

        Ca ca = caLoginService.loginByPhoneNumber(phoneNumber);
        //生成令牌
        String token = caLoginService.createToken(ca);

        return AjaxResult.success(Constants.TOKEN,token);
    }

}
